package org.etom.impl.ui.jobs;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.osgi.util.NLS;
import org.etom.connector.datatypes.IMoodleModule;
import org.etom.impl.ui.Activator;
import org.etom.impl.ui.util.CommonMessages;
import org.etom.impl.ui.util.Logger;
import org.etom.impl.ui.util.ResourceManager;
import org.etom.impl.ui.views.ViewsMessages;

public class CopyJob extends Job {
  
  private IMoodleModule moodleModule;
  private IPath path;
  
  public CopyJob(IMoodleModule moodleModule, IPath path) {
    super(CommonMessages.Copy_Progress_Name);
    this.moodleModule = moodleModule;
    this.path = path;
  }

  @Override
  protected IStatus run(IProgressMonitor monitor) {
    try {
      ResourceManager.copyInto(moodleModule, path);
      return Status.OK_STATUS;
    } catch (Exception e) {
      String errorMessage = NLS.bind(ViewsMessages.Save_Module_Fail, moodleModule.getName());
      Logger.ERROR(errorMessage, e);
      return new Status(IStatus.ERROR, Activator.PLUGIN_ID, errorMessage);
    }
  }

}
